﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Linq.Utils;
using System.Linq.Dynamic;
using System.ComponentModel;

namespace MyForum.Business.Controllers
{
    public class SectionController : GenericController<Section, MyForumDataContext>
    {
        public static int GetAvailableSectionCount()
        {
            return GetAvailableSection().Count();
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static IQueryable<Section> GetAvailableSection()
        {
            var results = SelectAll().Where(s => s.Forums.Count > 0);

            return results;
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static IQueryable<Section> GetAvailableSection(int maximumRows, int startRowIndex)
        {
            #region Validation
            if (maximumRows <= 0)
            {
                throw new ArgumentOutOfRangeException("maximumRows");
            }

            if (startRowIndex < 0)
            {
                throw new ArgumentOutOfRangeException("startRowIndex");
            }
            #endregion

            return GetAvailableSection().Skip(startRowIndex).Take(maximumRows);
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static IQueryable<Section> GetAvailableSection(string sortExpression)
        {
            if (String.IsNullOrEmpty(sortExpression))
            {
                return GetAvailableSection();
            }

            return GetAvailableSection().OrderBy(sortExpression);
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static IQueryable<Section> GetAvailableSection(string sortExpression, int maximumRows, int startRowIndex)
        {
            #region Validation
            if (maximumRows <= 0)
            {
                throw new ArgumentOutOfRangeException("maximumRows");
            }

            if (startRowIndex < 0)
            {
                throw new ArgumentOutOfRangeException("startRowIndex");
            }
            #endregion

            if (String.IsNullOrEmpty(sortExpression))
            {
                return GetAvailableSection(maximumRows, startRowIndex);
            }

            return GetAvailableSection(sortExpression).Skip(startRowIndex).Take(maximumRows);
        }
    }
}
